package fr.uha.ensisa.kfetmanager.category.admin;

import fr.uha.ensisa.kfetmanager.category.CustomerCategory;
import fr.uha.ensisa.kfetmanager.category.CustomerCategoryManagerRemote;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author JC
 */
public class AdminCategoriesDelete extends HttpServlet {

    @EJB
    private CustomerCategoryManagerRemote categoryManager;
    private final String errorPage = "/error.jsp";
    private final String home = "/admin/categories";


    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String idString = request.getParameter("id");
        Long id;
        // Try to parse id parameter
        try {
            id = Long.parseLong(idString);
        } catch (Exception e) {
            this.getServletContext().setAttribute("errorMessage", "ID non valide");
            this.getServletContext().setAttribute("redirectTo", home);
            this.getServletContext().getRequestDispatcher(errorPage).forward(request, response);
            return;
        }

        // Get Customer object from ID and handle errors
        CustomerCategory c = categoryManager.getCategory(id);
        if (c == null) {
            this.getServletContext().setAttribute("errorMessage", "Aucune cat&eacute;gorie avec cette ID");
            this.getServletContext().setAttribute("redirectTo", home);
            this.getServletContext().getRequestDispatcher(errorPage).forward(request, response);
            return;
        }

        // Perform deletion
        try {
            categoryManager.deleteCategory(c);
        } catch (Exception e) {
            this.getServletContext().setAttribute("errorMessage", "Erreur lors de la suppression de cat&eacute;gorie : " + e.getLocalizedMessage());
            this.getServletContext().setAttribute("redirectTo", home);
            this.getServletContext().getRequestDispatcher(errorPage).forward(request, response);
            return;
        }

        // Redirect to home page
        response.sendRedirect(getServletContext().getContextPath() + home);
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
